/**  
* @Title: CrSysRuleService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年10月16日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.mapper.cityRun.CrSysRuleGroupMapper;
import com.lemon.mapper.cityRun.CrSysRuleMapper;
import com.lemon.model.cityRun.CrSysRule;
import com.lemon.model.cityRun.CrSysRuleGroup;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: CrSysRuleService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年10月16日
 * @版本 V 1.0 
 */
@Service
public class CrSysRuleServiceImpl implements CrSysRuleService {
	@Resource
	private CrSysRuleMapper ruleMapper;
	@Resource
	private CrSysRuleGroupMapper groupMapper;

	/**
	 * <p>
	 * Title: selectListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param rule
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月16日
	 */
	@Override
	public List<CrSysRule> selectListBySelective(CrSysRule rule) throws Exception {

		PageHelper.startPage(rule.getPageNum() , rule.getPageSize());
		List<CrSysRule> list = ruleMapper.selectListBySelective(rule);
		return list;
	}

	/**
	 * <p>
	 * Title: insertSelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param rule
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月16日
	 */
	@Override
	public int insertSelective(CrSysRule rule) throws Exception {
		int selective = ruleMapper.insertSelective(rule);
		return selective;
	}

	/**
	 * <p>
	 * Title: selectByPrimaryKey
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param rule
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月17日
	 */
	@Override
	public CrSysRule selectByPrimaryKey(String id) throws Exception {
		CrSysRule rule = ruleMapper.selectByPrimaryKey(id);
		return rule;
	}

	/**
	 * <p>
	 * Title: updateByPrimaryKeySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param rule
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月17日
	 */
	@Override
	public int updateByPrimaryKeySelective(CrSysRule rule) throws Exception {
		int row = ruleMapper.updateByPrimaryKeySelective(rule);
		return row;
	}

	/**
	 * <p>
	 * Title: insertGroupSelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param group
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年10月17日
	 */
	@Override
	public int insertGroupSelective(CrSysRuleGroup group) throws Exception {

		String ids = group.getRuleIds();
		String idd = dformatRuleIds(ids);
		group.setRuleIds(idd);
		int row = groupMapper.insertSelective(group);
		return row;
		//		return 0;
	}

	/**
	 * <p>
	 * Title: selectGroupListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param group
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月17日
	 */
	@Override
	public List<CrSysRuleGroup> selectGroupListBySelective(CrSysRuleGroup group) throws Exception {
		PageHelper.startPage(group.getPageNum() , group.getPageSize());
		List<CrSysRuleGroup> list = groupMapper.selectListBySelective(group);
		for (int i = 0; i < list.size(); i++) {
			CrSysRuleGroup group2 = list.get(i);
			if (StringUtil.isNotBlank(group2.getRuleIds())) {
				String ids = eformatRuleIds(group2.getRuleIds());
				group2.setRuleIds(ids);
			}
		}
		return list;
	}

	/**
	 * <p>
	 * Title: updateGroupByPrimaryKeySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param group
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月17日
	 */
	@Override
	public int updateGroupByPrimaryKeySelective(CrSysRuleGroup group) throws Exception {
		String idd = dformatRuleIds(group.getRuleIds());
		group.setRuleIds(idd);
		return groupMapper.updateByPrimaryKeySelective(group);
	}

	/**
	 * <p>
	 * Title: selectGroupByPrimaryKey
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年10月17日
	 */
	@Override
	public CrSysRuleGroup selectGroupByPrimaryKey(String id) throws Exception {

		CrSysRuleGroup group = groupMapper.selectByPrimaryKey(id);
		String ids = eformatRuleIds(group.getRuleIds());
		group.setRuleIds(ids);
		return group;
	}

	public String dformatRuleIds(String ids) {
		if (StringUtil.isEmpty(ids)) {
			return ids;
		}
		String[] ruleIds = ids.split(",");
		String idd = "";
		for (int j = 0; j < ruleIds.length; j++) {
			long id = IdTypeHandler.decode(ruleIds[j]);
			idd += id;
			if (j != ruleIds.length - 1) {
				idd += ",";
			}
		}
		return idd;
	}

	public String eformatRuleIds(String ids) {
		if (StringUtil.isEmpty(ids)) {
			return ids;
		}
		String[] ruleIds = ids.split(",");
		String idd = "";
		for (int j = 0; j < ruleIds.length; j++) {
			String id = IdTypeHandler.encode(Long.parseLong(ruleIds[j]));
			idd += id;
			if (j != ruleIds.length - 1) {
				idd += ",";
			}
		}
		return idd;
	}
}
